package com.bachongLightNovel.system.service.impl;

import com.bachongLightNovel.model.pojo.system.Permission;
import com.bachongLightNovel.model.vo.system.PermissionVo;
import com.bachongLightNovel.system.mapper.SystemPermissionMapper;
import com.bachongLightNovel.system.mapper.SystemRolePermissionMapper;
import com.bachongLightNovel.system.service.SystemPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class SystemPermissionServiceImpl implements SystemPermissionService {

    @Autowired
    private SystemPermissionMapper systemPermissionMapper;

    @Autowired
    private SystemRolePermissionMapper systemRolePermissionMapper;

    /**
     * 权限列表树形查询
     * @return
     */
    @Override
    public List<Permission> findPage() {
        //查询出所有的数据
        List<Permission> list=systemPermissionMapper.selectAll();
        //封装成树形的结构
        List<Permission> treeList = toTreeList(list, 0L);
        return treeList;
    }

    /**
     * 将链式集合转换为树形集合
     * @param list
     * @param root
     * @return
     */
    private  List<Permission> toTreeList(List<Permission> list,Long root){
        ArrayList<Permission> permissions = new ArrayList<>();
        for (Permission permission : list) {
            if (permission.getParentId().equals(root)){
                permission.setChildren(toTreeList(list,permission.getId()));
                permissions.add(permission);
            }
        }
        return permissions;
    }

    /**
     * 获取动态菜单权限
     * @param userId:
     * @return List<PermissionVo>
     * @author vermouth
     */
    @Override
    @Transactional
    public List<PermissionVo> getPermissions(Long userId) {
        //根据当前操作用户的id查询相应的权限
        //如果是admin用户，则查询全部权限
        List<Permission>permissionList=null;
        if (userId.intValue()==1){
            permissionList=systemPermissionMapper.selectAll();
        }else {
            permissionList=systemPermissionMapper.selectByUserId(userId);
        }

        //封装成树形结构
        List<Permission> treeList = toTreeList(permissionList, 0L);
        //封装成要求返回的格式
        List<PermissionVo> permissionVos = buildMenus(treeList);
        return permissionVos;
    }

    /**
     * 将树形结构的数据封装成路由类型
     * @param treeList:
     * @return List<PermissionVo>
     * @author vermouth
     */
    private List<PermissionVo> buildMenus(List<Permission> treeList){
        ArrayList<PermissionVo> menus = new ArrayList<>();
        for (Permission permission : treeList) {
            PermissionVo vo = new PermissionVo();
            vo.setTitle(permission.getTitle());
            vo.setName(permission.getName());
            List<Permission> children = permission.getChildren();
            if (!CollectionUtils.isEmpty(children)){
                vo.setChildren(buildMenus(children));
            }
            menus.add(vo);
        }
        return menus;
    }

    /**
     * 根据角色id查询对应的权限信息
     * @param roleId :
     * @return Result
     */
    @Override
    public List<Long> getPermissionIdsByRoleId(Long roleId) {

        List<Long> permissionIds=systemRolePermissionMapper.selectByRoleId(roleId);
        return permissionIds;
    }
}
